<?php
/**
 * 优惠券管理
 * edition ：v1
 * create time：2021-06-08
 * author: acker
 */
namespace App\Admin\Controllers\Coupon;

use App\Admin\Actions\Coupon\Remind;
use App\Http\Controllers\Controller;
use App\Models\yb\UserCouponModel;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;


class UseCouponController extends Controller
{
    use ModelForm;

    protected $header = '优惠券使用记录';

    /**
     * 工作状态列表
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');
            $content->body($this->grid());

        });
    }

    public function grid(){

        $grid = new Grid(new UserCouponModel());

        $grid->model()->orderBy('created_at', 'DESC');

        $grid->column('id','编号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('coupon_name','优惠券名称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('User.nickname','用户昵称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('User.phone','用户电话')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('status','状态')->using(['0'=>'未使用','1'=>'已使用'], '未知')->dot([
            0 => 'danger',
            1 => 'info',
        ], 'success');


        $grid->column('usage_time','使用时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('created_at','领取时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });


        //搜索
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->like('User.nickname','用户昵称');
                $filter->like('User.phone','用户电话');
                $filter->equal('status', '状态')->select(['0'=>'未使用','1'=>'已使用']);

            });
            $filter->column(1/2, function ($filter) {
                $filter->between('usage_time', '使用时间')->datetime();
                $filter->between('created_at', '领取时间')->datetime();
            });

            $filter->disableIdFilter();
        });

        $grid->disableExport();

        $grid->fixColumns(1, -1);

        $grid->actions(function($actions){
            $actions->disableView();
        });

        $grid->disableActions();

        $grid->disableCreateButton();

        $grid->tools(function ($tools) {
            $tools->append(new Remind());
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        $grid->paginate(10);

        return $grid;
    }

}
